package qldc.pm.tiensu.service.impl;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import qldc.pm.tiensu.model.BienBanBanGiaoHoSo;
import qldc.pm.tiensu.model.CongViecBanGiao;
import qldc.pm.tiensu.model.DoiTuongTienSu;
import qldc.pm.tiensu.model.KetQuaTrichLuc;
import qldc.pm.tiensu.model.LichSuXoaAnTich;
import qldc.pm.tiensu.model.PhieuThongTinVeNguoiCoTienSu;
import qldc.pm.tiensu.model.TEMP_DoiTuongTienSu;
import qldc.pm.tiensu.model.TEMP_PhieuThongTinVeNguoiCoTienSu;
import qldc.pm.tiensu.model.TEMP_TienSu;
import qldc.pm.tiensu.model.TangThu;
import qldc.pm.tiensu.model.TienSu;
import qldc.pm.tiensu.model.YeuCauTrichLuc;
import qldc.pm.tiensu.service.BienBanBanGiaoHoSoLocalServiceUtil;
import qldc.pm.tiensu.service.CongViecBanGiaoLocalServiceUtil;
import qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil;
import qldc.pm.tiensu.service.KetQuaTrichLucLocalServiceUtil;
import qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil;
import qldc.pm.tiensu.service.PhieuThongTinVeNguoiCoTienSuLocalServiceUtil;
import qldc.pm.tiensu.service.TEMP_DoiTuongTienSuLocalServiceUtil;
import qldc.pm.tiensu.service.TEMP_PhieuThongTinVeNguoiCoTienSuLocalServiceUtil;
import qldc.pm.tiensu.service.TEMP_TienSuLocalServiceUtil;
import qldc.pm.tiensu.service.TangThuLocalServiceUtil;
import qldc.pm.tiensu.service.TienSuLocalServiceUtil;
import qldc.pm.tiensu.service.YeuCauTrichLucLocalServiceUtil;
import qldc.pm.tiensu.service.base.PhucHoiTienSuLocalServiceBaseImpl;
import qldc.pm.tiensu.service.persistence.TEMP_PhieuThongTinVeNguoiCoTienSuPersistence;
import qldc.pm.tiensu.service.persistence.TEMP_TienSuPersistence;
import qldc.pm.tiensu.service.persistence.TienSuPersistence;
import qldc.pm.tiensu.service.persistence.YeuCauTrichLucPersistence;
import qldc.pm.tiensu.utils.saoluudulieu.objects.BienBanBanGiaoHoSoJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.BienBanBanGiaoHoSoListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.CongViecBanGiaoJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.CongViecBanGiaoListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.DoiTuongTienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.DoiTuongTienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.KetQuaTrichLucJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.KetQuaTrichLucListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.LichSuXoaAnTichJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.LichSuXoaAnTichListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.PhieuThongTinVeNguoiCoTienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.PhieuThongTinVeNguoiCoTienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_DoiTuongTienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_DoiTuongTienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_PhieuThongTinVeNguoiCoTienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_PhieuThongTinVeNguoiCoTienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_TienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TEMP_TienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TangThuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TangThuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TienSuJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.TienSuListJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.YeuCauTrichLucJAXB;
import qldc.pm.tiensu.utils.saoluudulieu.objects.YeuCauTrichLucListJAXB;
import qldc.pm.tiensu.utils.saoluuphuchoi.PhucHoiUtils;
import qldc.pm.tiensu.utils.saoluuphuchoi.RootTienSuJAXB;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.Validator;

/**
 * The implementation of the phuc hoi tien su local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.tiensu.service.PhucHoiTienSuLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tiensu.service.base.PhucHoiTienSuLocalServiceBaseImpl
 * @see qldc.pm.tiensu.service.PhucHoiTienSuLocalServiceUtil
 */
public class PhucHoiTienSuLocalServiceImpl
    extends PhucHoiTienSuLocalServiceBaseImpl {

	  public void phucHoiDuLieu(long userId, File xmlFile) throws JAXBException, SystemException, IOException, PortalException{
		  System.out.println("File >>> "+xmlFile);
			if(null != xmlFile){
				//phuc hoi data
				byte[] xMLBytes = FileUtil.getBytes(xmlFile);
				if(null != xMLBytes){
					JAXBContext jaxbContext = JAXBContext.newInstance(RootTienSuJAXB.class);
					Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
					RootTienSuJAXB root = (RootTienSuJAXB) jaxbUnmarshaller.unmarshal(xmlFile);
					
					if(null != root){
						//phuc hoi data
						phucHoiBienBanBanGiaoHoSo(root.getBienBanBanGiaoHoSoList());
						phucHoiCongViecBanGiao(root.getCongViecBanGiaoList());
						phucHoiDoiTuongTienSu(root.getDoiTuongTienSuList());
						phucHoiKetQuaTrichLuc(root.getKetQuaTrichLucList());
						phucHoiLichSuXoaAnTich(root.getLichSuXoaAnTichList());
						phucHoiPhieuThongTinVeNguoiCoTienSu(root.getPhieuThongTinVeNguoiCoTienSuList());
						phucHoiTangThu(root.getTangThuList());
						phucHoiTEMP_DoiTuongTienSu(root.getTEMP_DoiTuongTienSuList());
						phucHoiTEMP_PhieuThongTinVeNguoiCoTienSu(root.getTEMP_PhieuThongTinVeNguoiCoTienSuList());
						phucHoiYeuCauTrichLuc(root.getYeuCauTrichLucList());
						phucHoiTEMP_TienSu(root.getTEMP_TienSuList());
						phucHoiTienSu(root.getTienSuList());
					}
					}
			
			} else {
				System.out.println("File is empty");
			}
	   }
	  private void phucHoiBienBanBanGiaoHoSo(BienBanBanGiaoHoSoListJAXB BienBanBanGiaoHoSoList) throws SystemException {
			bienBanBanGiaoHoSoPersistence.removeAll();
			//add new
			if (Validator.isNull(BienBanBanGiaoHoSoList) || Validator.isNull(BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo()))return;
			for (int i= 0;i< BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo().size();i++){
				BienBanBanGiaoHoSoJAXB jaxb = BienBanBanGiaoHoSoList.getBienBanBanGiaoHoSo().get(i);
				BienBanBanGiaoHoSo obj = bienBanBanGiaoHoSoPersistence.create(jaxb.getBienBanBanGiaoHoSoId());
				obj = PhucHoiUtils.convertFormBienBanBanGiaoHoSoToJAXB(jaxb, obj);
				BienBanBanGiaoHoSoLocalServiceUtil.addBienBanBanGiaoHoSo(obj);

			}
		}


		private void phucHoiCongViecBanGiao(CongViecBanGiaoListJAXB CongViecBanGiaoList) throws SystemException {
			congViecBanGiaoPersistence.removeAll();
			//add new
			if (Validator.isNull(CongViecBanGiaoList) || Validator.isNull(CongViecBanGiaoList.getCongViecBanGiao()))return;
			for (int i= 0;i< CongViecBanGiaoList.getCongViecBanGiao().size();i++){
				CongViecBanGiaoJAXB jaxb = CongViecBanGiaoList.getCongViecBanGiao().get(i);
				CongViecBanGiao obj = congViecBanGiaoPersistence.create(jaxb.getCongViecBanGiaoId());
				obj = PhucHoiUtils.convertFormCongViecBanGiaoToJAXB(jaxb, obj);
				CongViecBanGiaoLocalServiceUtil.addCongViecBanGiao(obj);

			}
		}
		private void phucHoiDoiTuongTienSu(DoiTuongTienSuListJAXB DoiTuongTienSuList) throws SystemException {
			doiTuongTienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(DoiTuongTienSuList) || Validator.isNull(DoiTuongTienSuList.getDoiTuongTienSu()))return;
			for (int i= 0;i< DoiTuongTienSuList.getDoiTuongTienSu().size();i++){
				DoiTuongTienSuJAXB jaxb = DoiTuongTienSuList.getDoiTuongTienSu().get(i);
				DoiTuongTienSu obj =doiTuongTienSuPersistence.create(jaxb.getDoiTuongTienSuId());
				obj = PhucHoiUtils.convertFormDoiTuongTienSuToJAXB(jaxb, obj);
				DoiTuongTienSuLocalServiceUtil.addDoiTuongTienSu(obj);

			}
		}
		private void phucHoiKetQuaTrichLuc(KetQuaTrichLucListJAXB KetQuaTrichLucList) throws SystemException {
			ketQuaTrichLucPersistence.removeAll();
			//add new
			if (Validator.isNull(KetQuaTrichLucList) || Validator.isNull(KetQuaTrichLucList.getKetQuaTrichLuc()))return;
			for (int i= 0;i< KetQuaTrichLucList.getKetQuaTrichLuc().size();i++){
				KetQuaTrichLucJAXB jaxb = KetQuaTrichLucList.getKetQuaTrichLuc().get(i);
				KetQuaTrichLuc obj = ketQuaTrichLucPersistence.create(jaxb.getKetQuaTrichLucId());
				obj = PhucHoiUtils.convertFormKetQuaTrichLucToJAXB(jaxb, obj);
				KetQuaTrichLucLocalServiceUtil.addKetQuaTrichLuc(obj);

			}
		}
		private void phucHoiLichSuXoaAnTich(LichSuXoaAnTichListJAXB LichSuXoaAnTichList) throws SystemException {
			lichSuXoaAnTichPersistence.removeAll();
			//add new
			if (Validator.isNull(LichSuXoaAnTichList) || Validator.isNull(LichSuXoaAnTichList.getLichSuXoaAnTich()))return;
			for (int i= 0;i< LichSuXoaAnTichList.getLichSuXoaAnTich().size();i++){
				LichSuXoaAnTichJAXB jaxb = LichSuXoaAnTichList.getLichSuXoaAnTich().get(i);
				LichSuXoaAnTich obj = lichSuXoaAnTichPersistence.create(jaxb.getLichSuXoaAnTichId());
				obj = PhucHoiUtils.convertFormLichSuXoaAnTichToJAXB(jaxb, obj);
				LichSuXoaAnTichLocalServiceUtil.addLichSuXoaAnTich(obj);

			}
		}
		private void phucHoiPhieuThongTinVeNguoiCoTienSu(PhieuThongTinVeNguoiCoTienSuListJAXB PhieuThongTinVeNguoiCoTienSuList) throws SystemException {
			phieuThongTinVeNguoiCoTienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(PhieuThongTinVeNguoiCoTienSuList) || Validator.isNull(PhieuThongTinVeNguoiCoTienSuList.getPhieuThongTinVeNguoiCoTienSu()))return;
			for (int i= 0;i< PhieuThongTinVeNguoiCoTienSuList.getPhieuThongTinVeNguoiCoTienSu().size();i++){
				PhieuThongTinVeNguoiCoTienSuJAXB jaxb = PhieuThongTinVeNguoiCoTienSuList.getPhieuThongTinVeNguoiCoTienSu().get(i);
				PhieuThongTinVeNguoiCoTienSu obj = phieuThongTinVeNguoiCoTienSuPersistence.create(jaxb.getPhieuThongTinVeNguoiCoTienSuId());
				obj = PhucHoiUtils.convertFormPhieuThongTinVeNguoiCoTienSuToJAXB(jaxb, obj);
				PhieuThongTinVeNguoiCoTienSuLocalServiceUtil.addPhieuThongTinVeNguoiCoTienSu(obj);

			}
		}
		private void phucHoiTangThu(TangThuListJAXB TangThuList) throws SystemException {
			tangThuPersistence.removeAll();
			//add new
			if (Validator.isNull(TangThuList) || Validator.isNull(TangThuList.getTangThu()))return;
			for (int i= 0;i< TangThuList.getTangThu().size();i++){
				TangThuJAXB jaxb = TangThuList.getTangThu().get(i);
				TangThu obj = tangThuPersistence.create(jaxb.getTangThuId());
				obj = PhucHoiUtils.convertFormTangThuToJAXB(jaxb, obj);
				TangThuLocalServiceUtil.addTangThu(obj);

			}
		}
		private void phucHoiTEMP_DoiTuongTienSu(TEMP_DoiTuongTienSuListJAXB TEMP_DoiTuongTienSuList) throws SystemException {
			temp_DoiTuongTienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_DoiTuongTienSuList) || Validator.isNull(TEMP_DoiTuongTienSuList.getTEMP_DoiTuongTienSu()))return;
			for (int i= 0;i< TEMP_DoiTuongTienSuList.getTEMP_DoiTuongTienSu().size();i++){
				TEMP_DoiTuongTienSuJAXB jaxb = TEMP_DoiTuongTienSuList.getTEMP_DoiTuongTienSu().get(i);
				TEMP_DoiTuongTienSu obj = temp_DoiTuongTienSuPersistence.create(jaxb.getDoiTuongTienSuId());
				obj = PhucHoiUtils.convertFormTEMP_DoiTuongTienSuToJAXB(jaxb, obj);
				TEMP_DoiTuongTienSuLocalServiceUtil.addTEMP_DoiTuongTienSu(obj);

			}
		}
		private void phucHoiTEMP_PhieuThongTinVeNguoiCoTienSu(TEMP_PhieuThongTinVeNguoiCoTienSuListJAXB TEMP_PhieuThongTinVeNguoiCoTienSuList) throws SystemException {
			temp_PhieuThongTinVeNguoiCoTienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_PhieuThongTinVeNguoiCoTienSuList) || Validator.isNull(TEMP_PhieuThongTinVeNguoiCoTienSuList.getTEMP_PhieuThongTinVeNguoiCoTienSu()))return;
			for (int i= 0;i< TEMP_PhieuThongTinVeNguoiCoTienSuList.getTEMP_PhieuThongTinVeNguoiCoTienSu().size();i++){
				TEMP_PhieuThongTinVeNguoiCoTienSuJAXB jaxb = TEMP_PhieuThongTinVeNguoiCoTienSuList.getTEMP_PhieuThongTinVeNguoiCoTienSu().get(i);
				TEMP_PhieuThongTinVeNguoiCoTienSu obj = temp_PhieuThongTinVeNguoiCoTienSuPersistence.create(jaxb.getPhieuThongTinVeNguoiCoTienSuId());
				obj = PhucHoiUtils.convertFormTEMP_PhieuThongTinVeNguoiCoTienSuToJAXB(jaxb, obj);
				TEMP_PhieuThongTinVeNguoiCoTienSuLocalServiceUtil.addTEMP_PhieuThongTinVeNguoiCoTienSu(obj);

			}
		}

		private void phucHoiTEMP_TienSu(TEMP_TienSuListJAXB TEMP_TienSuList) throws SystemException {
			temp_TienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(TEMP_TienSuList) || Validator.isNull(TEMP_TienSuList.getTEMP_TienSu()))return;
			for (int i= 0;i< TEMP_TienSuList.getTEMP_TienSu().size();i++){
				TEMP_TienSuJAXB jaxb = TEMP_TienSuList.getTEMP_TienSu().get(i);
				TEMP_TienSu obj = temp_TienSuPersistence.create(jaxb.getTienSuId());
				obj = PhucHoiUtils.convertFormTEMP_TienSuToJAXB(jaxb, obj);
				TEMP_TienSuLocalServiceUtil.addTEMP_TienSu(obj);

			}
		}

		private void phucHoiTienSu(TienSuListJAXB TienSuList) throws SystemException {
			tienSuPersistence.removeAll();
			//add new
			if (Validator.isNull(TienSuList) || Validator.isNull(TienSuList.getTienSu()))return;
			for (int i= 0;i< TienSuList.getTienSu().size();i++){
				TienSuJAXB jaxb = TienSuList.getTienSu().get(i);
				TienSu obj = tienSuPersistence.create(jaxb.getTienSuId());
				obj = PhucHoiUtils.convertFormTienSuToJAXB(jaxb, obj);
				TienSuLocalServiceUtil.addTienSu(obj);

			}
		}

		private void phucHoiYeuCauTrichLuc(YeuCauTrichLucListJAXB YeuCauTrichLucList) throws SystemException {
			yeuCauTrichLucPersistence.removeAll();
			//add new
			if (Validator.isNull(YeuCauTrichLucList) || Validator.isNull(YeuCauTrichLucList.getYeuCauTrichLuc()))return;
			for (int i= 0;i< YeuCauTrichLucList.getYeuCauTrichLuc().size();i++){
				YeuCauTrichLucJAXB jaxb = YeuCauTrichLucList.getYeuCauTrichLuc().get(i);
				YeuCauTrichLuc obj = yeuCauTrichLucPersistence.create(jaxb.getYeuCauTrichLucId());
				obj = PhucHoiUtils.convertFormYeuCauTrichLucToJAXB(jaxb, obj);
				YeuCauTrichLucLocalServiceUtil.addYeuCauTrichLuc(obj);

			}
		}
}
